﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Inovout.ExcelReport;
using Inovout;
using DocumentFormat.OpenXml.Packaging;
using System.IO.Packaging;
using System.IO;

namespace ExcelReportDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string excelTemplageFileName = "ExcelReportDemo.xltx";
            string dataSourceSetPath = @"report\DataSourceSet.xml";
            using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(excelTemplageFileName, true))
            {
                Uri dataSourceSetPartUri = PackUriHelper.CreatePartUri(
                                         new Uri(dataSourceSetPath, UriKind.Relative));
                PackagePart dataSourceSetPart =
                    spreadsheetDocument.Package.CreatePart(dataSourceSetPartUri,
                                   System.Net.Mime.MediaTypeNames.Text.Xml);
                using (FileStream source = new FileStream(
                      "ReportExcelPackage/report/dataSourceSet.xml", FileMode.Open, FileAccess.Read))
                {
                    const int bufSize = 0x1000;
                    byte[] buf = new byte[bufSize];
                    int bytesRead = 0;
                    Stream target = dataSourceSetPart.GetStream();
                    while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
                        target.Write(buf, 0, bytesRead);

                }
                dataSourceSetPart.CreateRelationship(
                                         dataSourceSetPart.Uri,
                                         TargetMode.Internal,
                                         @"http://schemas.microsoft.com/opc/2006/sample/document");

            }

            ComponentRegistry.Initialize();
            ExcelReportDocument excelReport = new ExcelReportDocument(excelTemplageFileName);
            excelReport.Save(DateTime.Now.ToString("Mdhms") + ".xlsx");

            Console.Read();
        }
    }
}
